package com.qzsoft.zhjd.materiel.modules.materielconsume.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzsoft.zhjd.common.annotation.ZhjdDataFilter;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;
import com.qzsoft.zhjd.materiel.modules.materielconsume.dto.MaterielConsumeInventoryDto;
import com.qzsoft.zhjd.materiel.modules.materielconsume.dto.MaterielConsumeListDto;
import com.qzsoft.zhjd.materiel.modules.materielconsume.dto.MaterielConsumeProdDto;
import com.qzsoft.zhjd.materiel.modules.materielconsume.entity.MaterielConsumeEntity;
import com.qzsoft.zhjd.materiel.modules.materielconsume.enums.MaterielConsumeEnums;
import com.qzsoft.zhjd.materiel.modules.materielconsume.mapper.MaterielConsumeMapper;
import com.qzsoft.zhjd.materiel.modules.materielconsume.service.MaterielConsumeService;

/**
 * @ClassName MaterielConsumeServiceImpl
 * @Description 材料消耗业务接口实现
 * @author linhuawei
 * @since 2022-04-05
 */
@Service
public class MaterielConsumeServiceImpl extends ServiceImpl<MaterielConsumeMapper, MaterielConsumeEntity>
        implements MaterielConsumeService {

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public IPage<MaterielConsumeListDto> pageDto(Map<String, Object> params) {
        IPage<MaterielConsumeListDto> page = baseServiceComponent.getPage(params, MaterielConsumeListDto.class);
        return baseMapper.queryDto(page, params);
    }

    /**
     * TODO 关于硬编码，后续需要处理
     */
    @Override
    public boolean save(MaterielConsumeEntity entity) {
        // 删除已经存在的数据
        QueryWrapper<MaterielConsumeEntity> queryWrapper = new QueryWrapper<MaterielConsumeEntity>();
        queryWrapper.eq("base_code", entity.getBaseCode());
        queryWrapper.eq("begin_time", entity.getBeginTime());
        queryWrapper.eq("end_time", entity.getEndTime());
        baseMapper.delete(queryWrapper);

        // 获取盘点数据
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("baseCode", entity.getBaseCode());
        params.put("startDate", entity.getBeginTime());
        params.put("endDate", entity.getEndTime());
        List<MaterielConsumeInventoryDto> inventoryList = baseMapper.queryInventoryDto(params);
        if (null == inventoryList || 0 == inventoryList.size()) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "未找到盘点数据");
        }

        // 获取生产数据
        MaterielConsumeProdDto proDto = baseMapper.queryProdDto(params);

        // 拼接数据
        MaterielConsumeEntity gravel = null;// 砂石料
        MaterielConsumeEntity cement = null;// 水泥
        MaterielConsumeEntity asphalt = null;// 沥青
        for (MaterielConsumeInventoryDto dto : inventoryList) {
            if (MaterielConsumeEnums.asphalt.getMaterielItemPkid().equals(dto.getMaterielItemPkid())) {
                asphalt = new MaterielConsumeEntity();
                asphalt.setBeginTime(entity.getBeginTime());
                asphalt.setEndTime(entity.getEndTime());
                asphalt.setBaseCode(entity.getBaseCode());
                asphalt.setMaterielItemId(MaterielConsumeEnums.asphalt.getMaterielItemId());
                asphalt.setBeginBalance(dto.getBeginBalance());
                asphalt.setEndBalance(dto.getEndBalance());
                asphalt.setIncome(dto.getIncome());
                asphalt.setIncomeYear(dto.getIncomeYear());
                asphalt.setExpend(dto.getExpend());
            } else if (MaterielConsumeEnums.cement.getMaterielItemPkid().equals(dto.getMaterielItemPkid())) {
                cement = new MaterielConsumeEntity();
                cement.setBeginTime(entity.getBeginTime());
                cement.setEndTime(entity.getEndTime());
                cement.setBaseCode(entity.getBaseCode());
                cement.setMaterielItemId(MaterielConsumeEnums.cement.getMaterielItemId());
                cement.setBeginBalance(dto.getBeginBalance());
                cement.setEndBalance(dto.getEndBalance());
                cement.setIncome(dto.getIncome());
                cement.setIncomeYear(dto.getIncomeYear());
                cement.setExpend(dto.getExpend());
            } else if (MaterielConsumeEnums.gravel.getMaterielItemPkid().equals(dto.getMaterielItemPkid())) {
                gravel = new MaterielConsumeEntity();
                gravel.setBeginTime(entity.getBeginTime());
                gravel.setEndTime(entity.getEndTime());
                gravel.setBaseCode(entity.getBaseCode());
                gravel.setMaterielItemId(MaterielConsumeEnums.gravel.getMaterielItemId());
                gravel.setBeginBalance(dto.getBeginBalance());
                gravel.setEndBalance(dto.getEndBalance());
                gravel.setIncome(dto.getIncome());
                gravel.setIncomeYear(dto.getIncomeYear());
                gravel.setExpend(dto.getExpend());
            }
        }

        // 生产数据
        if (null != proDto) {
            if (null != asphalt) {
                asphalt.setPlanWeigth(proDto.getAsphaltPlan());
                asphalt.setProdWeigth(proDto.getAsphaltProd());
            }
            if (null != cement) {
                cement.setPlanWeigth(proDto.getCementPlan());
                cement.setProdWeigth(proDto.getCementProd());
            }

            if (null != gravel) {
                gravel.setPlanWeigth(proDto.getGravelPlan());
                gravel.setProdWeigth(proDto.getGravelProd());
            }
        }

        // 执行添加操作
        List<MaterielConsumeEntity> list = new ArrayList<MaterielConsumeEntity>();
        if(null != cement) {
            list.add(cement);
        }
        if(null != gravel) {
            list.add(gravel);
        }
        if(null != asphalt) {
            list.add(asphalt);
        }
        super.saveBatch(list);
        return true;
    }
}
